var test = {
    addOrUpdate: function (obj) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.$;
            var content = '<form class="layui-form" id="test-form">';
            content += '<div class="layui-form-item"><label class="layui-form-label">姓名</label><div class="layui-input-block"><input type="text" name="name" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input" /></div></div>';
            content += '<div class="layui-form-item"><label class="layui-form-label">性别</label><div class="layui-input-block"><select name="sex"><option value="1">男</option><option value="2">女</option></select></div></div>';
            content += '<div class="layui-form-item"><label class="layui-form-label">分数</label><div class="layui-input-block"><input type="number" name="score" required  lay-verify="required" placeholder="请输入分数" autocomplete="off" class="layui-input" /></div></div>';
            content += '</form>';
            layer.open({
                content: content
                , btn: ['确定', '取消']
                , success: function (layero, index) {
                    if (obj) {
                        for (var o in obj) {
                            $('#test-form [name="' + o + '"]').val(obj[o]);
                        }
                    }
                    form.render();
                }
                , yes: function (index, layero) {
                    layer.msg("按钮【确定】的回调");
                }
                , btn2: function (index, layero) {
                    layer.msg("按钮【取消】的回调");
                    //return false 开启该代码可禁止点击该按钮关闭
                }
                , cancel: function () {
                    //右上角关闭回调
                    layer.msg("右上角关闭回调");
                    //return false 开启该代码可禁止点击该按钮关闭
                }
            });
        });
    }
};
layui.use(['layer', 'table', 'form'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$;
    table.render({
        elem: '#demo'
        , url: '/user/userList'
        , page: true
        , limits: [10, 15, 20]
        , cols: [[
            {field: 'id', title: 'ID', sort: true, fixed: 'left'}
            , {field: 'name', title: '用户名'}
            , {
                field: 'sex', title: '性别', sort: true, templet: function (d) {
                    return d.sex == 1 ? '男' : '女';
                }
            }
            , {field: 'score', title: '评分', sort: true}
            , {
                field: 'isPass', title: '是否及格', templet: function (d) {
                    if (d.score < 60) {
                        return "<font color='#FF0000'>不及格</font>";
                    }
                    return "<font color='#000000'>及格</font>";
                }
            }
            , {
                field: 'option', title: '操作', templet: function (d) {
                    //var obj = {'id':d.id,'name':d.name,'sex':d.sex,'score':d.score};
                    return '<a href="javascript:;" onclick="test.addOrUpdate(' + JSON.stringify(d).replace(/"/g, '&quot;') + ')">编辑</a>';
                }
            }
        ]]
    });

    $('#test-add').click(function () {
        test.addOrUpdate();
    });

});